package com.tiyende.credit.loop.ui.presenter

import android.Manifest
import com.lib.common.permission.CommonPermissions
import com.lib.common.permission.Permission
import com.tiyende.credit.loop.common.CommonPresenter
import com.tiyende.credit.loop.configs.TiyendeApiPath
import com.tiyende.credit.loop.configs.TiyendeApiUrl
import com.tiyende.credit.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.utils.helper.TiyendeCreditStatusHelper
import com.tiyende.credit.loop.utils.helper.TiyendeProcessHelper
import com.tiyende.credit.loop.utils.helper.TiyendeProcessStatusHelper
import com.tiyende.credit.loop.utils.helper.TiyendeUserDecInvok
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/9/10 13:41
 * @Annotation：未授信、未借款、已借款
 */
class TiyendeWithdrawPresenter(private val iView: IViewContract.ITiyendeWithdrawView): CommonPresenter(iView) {
    var amountInfo: JSONObject? = null

    /**
     * 获取额度信息
     */
    fun queryTiyendeLimitCreditAction(){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_LIMIT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    amountInfo = obj
                    iView.showTiyendeLimitInfo(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 获取授信状态
     */
    private fun queryTiyendeCreditStatusAction(data: JSONObject){
        // 进件信息未完成
        if (TiyendeProcessHelper.queryTiyendeAuthenticationStepList().size > 0){
            iView.showTiyendeLimitCreditBeforViews(data)
            return
        }

        val isCreditReviewing = TiyendeCreditStatusHelper.TiyendeCreditStatusRepository().isCreditReviewing()
        //授信中(未处理)
        if (isCreditReviewing){
            iView.showTiyendeLimitCreditBeforViews(data)
            return
        }

        val creditStatus = TiyendeProcessHelper.queryTiyendeAuthenticationBeforeRiskStatus()
        val isCreditRejected = TiyendeCreditStatusHelper.TiyendeCreditStatusRepository().isCreditRejected(creditStatus)
        //已拒绝
        if (isCreditRejected){
            iView.showTiyendeLimitCreditBeforViews(data)
            return
        }

        // 授信通过 && 进件信息已完成 - 获取额度
        iView.showTiyendeLimitCreditedViews(data)
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkTiyendeWithdrawPermissionsAgree(){
        //权限页操作状态 : 0未操作  1已同意  -1拒绝
        val permissionStatus = TiyendeSharedHelper.queryTiyendeSharedInt(TiyendeSharedKey.TIYENDE_PERMISSION_PAGE_STATUS)
        //权限列表未操作 -> 进入权限列表
        if (permissionStatus == 0){
            iView.showTiyendeWithdrawPermissionPage()
            return
        }

        //是否是广告渠道
        val isAdSource = TiyendeSharedHelper.queryTiyendeSharedBoolean(TiyendeSharedKey.TIYENDE_IS_ADSOURCE)
        //广告渠道 && 权限列表未同意 -> 进入权限列表
        if (isAdSource && permissionStatus == -1){
            iView.showTiyendeWithdrawPermissionPage()
            return
        }
        //广告渠道 && 未同意sms权限 -> 进入权限列表
        if (isAdSource && !CommonPermissions.isGranted(iView.getAct(), Permission.READ_SMS)){
            iView.showTiyendeWithdrawPermissionPage()
            return
        }

        //订单被拒绝
        val applyStatus = TiyendeProcessHelper.queryTiyendeAuthenticationApplyStatus()
        val orderStatus = TiyendeProcessHelper.queryTiyendeAuthenticationOrderStatus()
        val isRejected = TiyendeProcessStatusHelper.StatusRepository(applyStatus,orderStatus).isRejected()
        if (isRejected){
            iView.showTiyendeWithdrawOrderRejectedDialog()
            return
        }
        showTiyendeWithdrawPermissionSuccess()
    }

    /**
     * 权限检查成功
     */
    private fun showTiyendeWithdrawPermissionSuccess(){
        //权限页操作状态 : 0未操作  1已同意  -1拒绝
        val permissionStatus = TiyendeSharedHelper.queryTiyendeSharedInt(TiyendeSharedKey.TIYENDE_PERMISSION_PAGE_STATUS)
        //已同意权限列表 && 已同意SMS权限 -> 上报设备数据
        if (permissionStatus == 1 && CommonPermissions.isGranted(iView.getAct(), Manifest.permission.READ_SMS)){
            //上报设备数据
            TiyendeUserDecInvok.queryTiyendeDeviceInfoTypesAction()
        }
        //进件信息未完成 - 进件信息页面
        if (TiyendeProcessHelper.queryTiyendeAuthenticationStepList().size > 0){
            iView.showTiyendeWithdrawAUthenticationStepsPage()
            return
        }

        val creditStatus = TiyendeProcessHelper.queryTiyendeAuthenticationBeforeRiskStatus()
        val isCreditRejected = TiyendeCreditStatusHelper.TiyendeCreditStatusRepository().isCreditRejected(creditStatus)
        //授信被拒绝 - 拒绝页面
        if (isCreditRejected){
            iView.showTiyendeWithdrawCreditRejectedPage()
            return
        }

        val isCreditReviewing = TiyendeCreditStatusHelper.TiyendeCreditStatusRepository().isCreditReviewing()
        //进件信息已完成 && 授信中 - 授信状态查询页面
        if (isCreditReviewing){
            iView.showTiyendeWithdrawCreditReviewPage()
            return
        }

        //进件信息已完成 && 授信已完成 - 申贷试算页面
        iView.showTiyendeWithdrawApplyPage()
    }
}